package com.ithuameng.admin.app.system.service.impl;

import com.ithuameng.admin.app.system.model.AdminLoginUser;
import com.ithuameng.admin.app.system.model.User;
import com.ithuameng.admin.app.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * AdminUserDetailService
 *
 * @author ithuameng
 */
@RequiredArgsConstructor
@Service("adminUserDetailService")
public class AdminUserDetailService implements UserDetailsService {

	@Resource
	private IUserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userService.getUserByUsername(username);
		if (user == null) {
			return null;
		}
		return new AdminLoginUser(user.getId(), user.getUsername(), user.getPassword());
	}
}
